home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10273 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Tradition or what?
  5. Date: Sat, 16 Mar 1996 14:26:49 GMT
  6. Organization: Netcom
  7. Message-ID: <314ac1ef.49220064@nntp.ix.netcom.com>
  8. References: <4g0elg$mdr@redstone.interpath.net> <4hpd8a$d70@alterdial.UU.NET> <1996Mar8.153250.115645@kuhub.cc.ukans.edu>
  9. NNTP-Posting-Host: ix-dc8-04.ix.netcom.com
  10. X-NETCOM-Date: Sat Mar 16  6:27:11 AM PST 1996
  11. X-Newsreader: Forte Agent .99d/32.182
  12.  
  13. anh@kuhub.cc.ukans.edu wrote:
  14.  
  15. > Well, I found one good use of magic numbers such as when one needs a 
  16. > localized temporary buffer of data.
  17. > int func()
  18. > {
  19. >     FILE *fp;
  20. >     char buf[15+1];
  21. >     
  22. >     ...
  23. >     fgets(buf,15,fp);
  24. > }
  25. > Well, if I know the data is always going to be less than 15 or whatever, 
  26. > there is really no need to use a #define here.
  27.  
  28. Non sequitur.  Whether you know that the data is going to be less than
  29. 15 is immaterial.  In this situration I'd very rarely use a #define
  30. for the size of the buffer, but I would absolutely never repeat the
  31. size as a literal later in the code.  My preference is something like
  32.  
  33.     int func()
  34.     {
  35.       FILE *fp;
  36.       char buf[15];
  37.      
  38.        ...
  39.  
  40.        fgets(buf, sizeof buf, fp);
  41.     }
  42.  
  43. Note that I've changed the size of the buffer to 15 on the assumption
  44. that the intention is that the size of the data be at most 14
  45. characters excluding the terminating nul.
  46.  
  47. Michael M Rubenstein
  48.